package com.qkl.common.publics.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;

@Data
@TableName(value = "weixin_info")
public class WXInfoEntity {

    private static final int DELAY_SECONDS = 2;

    @TableId(type= IdType.ASSIGN_ID)
    private String appid;

    private String secret;

    private String appidName;

    private String accessToken;

    private Integer expiresToken;

    private Long tokenTime;

    private Date updateTime;

    public boolean tokenExpires(){
        long now = System.currentTimeMillis();
        return null == accessToken || expiresToken == null || tokenTime == null || (now - tokenTime) > (expiresToken - DELAY_SECONDS) * 1000;
    }

    public int expiresSeconds(){
        if(null == expiresToken || null == tokenTime){
            return 0;
        }
        long seconds = (System.currentTimeMillis() - tokenTime)/1000;
        return (int)(expiresToken - seconds - DELAY_SECONDS);
    }
}
